package xiaolong.zhou.spring_demo.Service.achieve;

import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class Captcha {
    @Autowired
    private  RedisTemplate<String,String> redisTemplate;
    public Map<String, Object> createCode() throws IOException, FontFormatException {
        SpecCaptcha captcha = new SpecCaptcha(130, 45, 6);
        captcha.setCharType(com.wf.captcha.base.Captcha.TYPE_DEFAULT);
        captcha.setFont(com.wf.captcha.base.Captcha.FONT_1);
        String uuid = String.valueOf(UUID.randomUUID());
//      session.setAttribute(uuid, captcha.text().toLowerCase());
//      session.setMaxInactiveInterval(600);//十分钟过期
        //session不生效设置redis存储
        redisTemplate.opsForValue().set(uuid,captcha.text().toLowerCase());//不区分大小写
        redisTemplate.expire(uuid,600, TimeUnit.SECONDS);
        Map<String, Object> info = new HashMap<>();
        info.put("captcha", captcha.toBase64());
        info.put("session_uuid", uuid);
        return info;
    }
}
